package com.marver.sys.service;

import com.marver.common.dao.BaseDAOImpl;
import com.marver.common.enumeration.IpTypEnum;
import com.marver.common.result.Result;
import com.marver.common.result.ResultStatus;
import com.marver.common.utils.JedisUtils;
import com.marver.entity.IpConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: ip 黑白名单 service
 * @Author: Marver
 * @date: 2018-04-23 16:02
 **/
@Service
public class IpConfigService extends BaseDAOImpl{
	@Autowired
	private JedisUtils jedisUtils;

    /**
     * 初始化ip黑白名单至redis中
     * @throws Exception
     */
    @Override
    protected void initDao() throws Exception {
        Map map = new HashMap();
        List<IpConfig> ipConfigList = (List<IpConfig>)this.listBySqlMap("ipLimt.queryBlackWhiteIp",map);
        ipConfigList.forEach(c->{
			jedisUtils.set(IpTypEnum.getByKey(c.getType())+c.getAppId(),c.getIpConfig(),0);
        });
    }
    
    /**
     * 获取黑白名单
     */
    public Result getBlackWhiteList(IpConfig Ip) {
    	 Result resultMsg;
    	 List<IpConfig> ipConfigList = (List<IpConfig>)this.listBySqlMap("ipLimt.selectByParameter",Ip);
    	 List<IpConfig> list=new ArrayList<IpConfig>();
	     for(IpConfig listIp : ipConfigList) {
	    	 IpConfig ipConfig= new IpConfig();
		     ipConfig.setAppId(listIp.getAppId());
		     ipConfig.setIpConfig(listIp.getIpConfig());
		     ipConfig.setType(listIp.getType());
		     ipConfig.setCreateTime(listIp.getCreateTime());
		     ipConfig.setCreateId(listIp.getCreateId());
		     ipConfig.setDelFlag(listIp.getDelFlag());
		     list.add(ipConfig);
	    } 
	    resultMsg = new Result(ResultStatus.SUCCESS.getErrorCode(),list);
        return resultMsg;
    }
    
    /**
	 * 添加黑白名单数据
	 * @return
	 */
    public Result addBlackWhiteIp(IpConfig Ip) {
	    	IpConfig ipConfig= new IpConfig();
		    ipConfig.setAppId(Ip.getAppId());
		    ipConfig.setIpConfig(Ip.getIpConfig());
		    ipConfig.setType(Ip.getType());
		    Date date = new Date();
		    ipConfig.setCreateTime(date);
		    ipConfig.setCreateId(Ip.getCreateId());
		    ipConfig.setDelFlag(0);
		    this.addBySqlMap("ipLimt.insert", ipConfig);
		    try {
				this.initDao();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return  new Result(ResultStatus.SUCCESS.getErrorCode());
    }
    
    /**
	 * 根据APPID增加黑白名单ip
	 * @return
	 */
    	public Result addBlackWhiteIpByAppId(IpConfig Ip) {
	    	IpConfig ipConfig= new IpConfig();
		    ipConfig.setAppId(Ip.getAppId());
		    ipConfig.setIpConfig(Ip.getIpConfig());
		    ipConfig.setType(Ip.getType());
		    ipConfig.setUpdateTime(new Date());
		    ipConfig.setUpdateId(Ip.getUpdateId());
		    this.updateBySqlMap("ipLimt.addWhiteOrBlack", ipConfig);
		    try {
				this.initDao();
			} catch (Exception e) {
				e.printStackTrace();
			}
		return  new Result(ResultStatus.SUCCESS.getErrorCode());
    }
    
    /**
   	 * 根据APPID删除黑白名单ip
   	 * @return
   	 */
       public Result delectBlackWhiteIpByAppId(IpConfig Ip) {
	       	IpConfig ipConfig= new IpConfig();
	   	    ipConfig.setAppId(Ip.getAppId());
	   	    ipConfig.setIpConfig(Ip.getIpConfig());
	   	    ipConfig.setType(Ip.getType());
	   	    Date date = new Date();
	   	    ipConfig.setUpdateTime(date);
	   	    ipConfig.setUpdateId(Ip.getUpdateId());
	        this.updateBySqlMap("ipLimt.delectWhiteOrBlack", ipConfig);
	        try {
				this.initDao();
			} catch (Exception e) {
				e.printStackTrace();
			}
   		return  new Result(ResultStatus.SUCCESS.getErrorCode());
       }
    
    
       /**
      	 * 根据ID删除黑白名单数据
      	 * @return
      	 */
          public Result delectBlackWhiteIpById(IpConfig Ip) {
   	       	IpConfig ipConfig= new IpConfig();
   	       	ipConfig.setDelFlag(1);
   	   	    Date date = new Date();
   	   	    ipConfig.setUpdateTime(date);
   	   	    ipConfig.setUpdateId(Ip.getUpdateId());
   	        this.updateBySqlMap("ipLimt.delect", ipConfig);
   	        try {
				this.initDao();
			} catch (Exception e) {
				e.printStackTrace();
			}
      		return  new Result(ResultStatus.SUCCESS.getErrorCode());
          }
    
    
}
